package com.p.lib.app;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.p.lib.util.DateUtil;
import com.p.lib.util.DensityUtil;
import com.p.lib.util.SharedPreferencesHelper;
import com.p.lib.util.ToastUtil;
import com.p.lib.util.language.LanguageUtil;
import com.p.lib.util.language.SupportLanguageUtil;
import com.p.lib.widget.fontview.textview.FontTextView;
import com.p.lib.widget.fontview.textview.FontTextView_B;

import java.util.Locale;

/**
 * Created by Administrator on 2018/5/31 0031.
 */
//method ID not in [0, 0xffff]: 65536  MultiDexApplication
public class BaseApplication extends MultiDexApplication {


//    private static BaseApplication instance;
    private static Context instance;

    public static String LOGIN_TOKEN = "0";
    public static final String ACTION_LOGIN_EXPIRES_HINT = "base_action_login_expires_hint";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);

    }

    @Override
    public void onCreate() {
        super.onCreate();

//        instance = this;

//        SharedPreferencesHelper.init(instance);
        SharedPreferencesHelper.init(this);

        //获取屏幕宽高
        getScreenWH();

//        //切换Context对象的中英文资源
//        instance = (BaseApplication) LanguageUtil.attachBaseContext(instance, getLanguage());

        //兼容Android 7.0系统
        //切换Context对象的中英文资源
//        Context newContext = LanguageUtil.attachBaseContext(instance, getLanguage());
//        if(newContext instanceof BaseApplication){
//            instance = (BaseApplication) newContext;
//        }

        instance = LanguageUtil.attachBaseContext(this, getLanguage());

    }


    private static Locale localeLanguage;

    /**
     * 是否显示英文资源
     * @return
     */
    public static boolean isEnglish(){

        if(localeLanguage == null && TextUtils.isEmpty(getLanguage())){
            Configuration config = instance.getResources().getConfiguration();
//            DisplayMetrics dm = instance.getResources().getDisplayMetrics();

            localeLanguage = config.locale;
            saveLanguage(localeLanguage.getLanguage(), false);

        }else{

            localeLanguage = SupportLanguageUtil.mSupportLanguages.get(getLanguage());

        }

        if(localeLanguage.getLanguage().equals(Locale.SIMPLIFIED_CHINESE.getLanguage())){
            return false;
        }

        return true;

    }

    public static void saveLanguage(String language, boolean isTrue){
        SharedPreferencesHelper.saveString("Language", language);

//        //切换Context对象的中英文资源
//        instance = (BaseApplication) LanguageUtil.attachBaseContext(instance, language);

        instance = LanguageUtil.attachBaseContext(instance, language);

        //兼容Android 7.0系统
        //切换Context对象的中英文资源
//        Context newContext = LanguageUtil.attachBaseContext(instance, getLanguage());
//        if(newContext instanceof BaseApplication){
//            instance = (BaseApplication) newContext;
//        }

        //重置资源
        DateUtil.weekFullNameDays = null;
        FontTextView.reset();
        FontTextView_B.reset();
        if(isTrue){
            localeLanguage = null;
        }
    }

    public static String getLanguage(){

        return SharedPreferencesHelper.getString("Language", "");
    }


    /**
     * 获取屏幕宽高
     */
    private void getScreenWH() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        if(dm.widthPixels < dm.heightPixels) {
            DensityUtil.screenWidth = dm.widthPixels;
            DensityUtil.screenHeight = dm.heightPixels;
        } else {
            DensityUtil.screenHeight = dm.widthPixels;
            DensityUtil.screenWidth = dm.heightPixels;
        }
    }

//    public static BaseApplication getInstance() {
//        return instance;
//    }

    public static Context getInstance() {
        return instance;
    }


    private static Toast toast;

    public static void toast(int resId){
        ToastUtil.showShort(getInstance(), resId);
    }

    public static void toastLong(int resId){
        ToastUtil.showLong(getInstance(), resId);
    }

    public static void toast(String s){
        ToastUtil.showShort(getInstance(), s);
    }

    public static void toastLong(String s){
        ToastUtil.showLong(getInstance(), s);
    }


}
